<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login">
    <div class="login-con">
        <H2>
          <Icon type="logo-buffer" />
          欢迎登录
        </H2>
        <div class="form-con">
          <login-form ref="login" :loading="loading" :btnText="btnText" @on-success-valid="handleSubmit"></login-form>
        </div>
    </div>
  </div>
</template>

<script>
import LoginForm from '_c/login-form'
import md5 from "@/libs/md5";
import { mapActions } from 'vuex'
export default {
  components: {
    LoginForm
  },
  data() {
    return {
      loading: false,
      btnText: '登录'
    }
  },
  methods: {
    ...mapActions([
      'handleLogin',
      'getUserInfo'
    ]),
    handleSubmit ({ userName, password, validCode, validCodeId }) {
      this.loading = true;
      this.btnText = '登录中...';
      password = md5.MD5(password);
      this.handleLogin({ userName, password, validCode, validCodeId }).then(res => {
        this.getUserInfo().then(res => {
          this.loading = false;
          this.btnText = '登录成功';
          this.$router.push({
            name: this.$config.homeName
          })
        }).catch(err => {
          this.loading = false;
          this.btnText = '登录';
        })
      }).catch(err => {
        // 错误时，刷新验证码
        this.loading = false;
        this.btnText = '登录';
        this.$refs.login.getCaptcha();
      })
    }
  },
  mounted() {
    console.log("login mounted begin");
  }
}
</script>

<style>

</style>
